package com.org.android.diary.utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioRecord;
import android.os.Build;
import android.support.v4.content.ContextCompat;

import pub.devrel.easypermissions.EasyPermissions;

public class PermissionUtil {
    public static PermissionUtil instance;
    public static int audioSource = 1;
    public static int sampleRateInHz = 44100;
    public static int channelConfig = 12;
    public static int audioFormat = 2;
    public static int bufferSizeInBytes = 0;

    public PermissionUtil() {
    }

    public static PermissionUtil getInstance() {
        if (instance == null) {
            instance = new PermissionUtil();
        }

        return instance;
    }

    public boolean havePermission(Context context, String s) {
        boolean hava = true;
        if (ContextCompat.checkSelfPermission(context, s) != 0) {
            hava = false;
        }

        return hava;
    }

    public static boolean isHasPermission(Context context) {
        bufferSizeInBytes = 0;
        bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
        AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);

        try {
            audioRecord.startRecording();
        } catch (IllegalStateException var3) {
            var3.printStackTrace();
        }

        if (audioRecord.getRecordingState() != 3) {
            context.startActivity(new Intent("android.settings.MANAGE_APPLICATIONS_SETTINGS"));
        }

        audioRecord.stop();
        audioRecord.release();
        return true;
    }

    public static boolean WriteAndReadPer(Activity mcontext, int REQUEST_CODE_SAVE_IMG) {
        boolean isHava = false;
        if (Build.VERSION.SDK_INT >= 19) {
            String[] mPermissionList = new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"};
            if (EasyPermissions.hasPermissions(mcontext, mPermissionList)) {
                isHava = true;
            } else {
                EasyPermissions.requestPermissions(mcontext, "保存图片需要读取sd卡的权限", REQUEST_CODE_SAVE_IMG, mPermissionList);
            }
        }

        return isHava;
    }

    public static boolean loginPermission(Activity activity, int LOGINPRESSION) {
        boolean isHava = false;
        if (Build.VERSION.SDK_INT >= 19) {
            String[] mPermissionList = new String[0];
            if (Build.VERSION.SDK_INT >= 23) {
                mPermissionList = new String[]{
                        "android.permission.READ_EXTERNAL_STORAGE",
                        "android.permission.WRITE_EXTERNAL_STORAGE",
                        "android.permission.RECORD_AUDIO",
                        "android.permission.CAMERA",
                        "android.permission.ACCESS_FINE_LOCATION",
                        "android.permission.ACCESS_COARSE_LOCATION",
                        "android.permission.SYSTEM_ALERT_WINDOW",
                        "android.permission.REQUEST_INSTALL_PACKAGES",
                        "android.permission.ACCESS_FINE_LOCATION",
                        "android.permission.READ_LOGS",
                        "android.permission.READ_PHONE_STATE",
                        "android.permission.SET_DEBUG_APP",
                        "android.permission.SYSTEM_ALERT_WINDOW",
                        "android.permission.ACCESS_NETWORK_STATE"};
            }
            if (EasyPermissions.hasPermissions(activity, mPermissionList)) {
                isHava = true;
            } else {
                isHava = false;
                EasyPermissions.requestPermissions(activity, "请允许对应权限", LOGINPRESSION, mPermissionList);
            }
        } else {
            isHava = true;
        }
        return isHava;
    }

    public static boolean checkRecorderPermissionMethod(Activity activity, int LOGINPRESSION) {
        boolean isHava = false;
        if (Build.VERSION.SDK_INT >= 19) {
            String[] mPermissionList = new String[]{
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.RECORD_AUDIO"};
            if (EasyPermissions.hasPermissions(activity, mPermissionList)) {
                isHava = true;
            } else {
                isHava = false;
                EasyPermissions.requestPermissions(activity, "请允许对应权限", LOGINPRESSION, mPermissionList);
            }
        } else {
            isHava = true;
        }
        return isHava;
    }

    public static boolean cameraPermission(Activity activity, int LOGINPRESSION) {
        boolean isHava = false;
        if (Build.VERSION.SDK_INT >= 19) {
            String[] mPermissionList = new String[]{
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.CAMERA"};
            if (EasyPermissions.hasPermissions(activity, mPermissionList)) {
                isHava = true;
            } else {
                EasyPermissions.requestPermissions(activity, "请开启您的照相机权限", LOGINPRESSION, mPermissionList);
            }
        } else {
            isHava = true;
        }
        return isHava;
    }
}
